package com.zxt.web.controller;

import com.zxt.domain.*;
import com.zxt.service.AddressService;
import com.zxt.service.CartService;
import com.zxt.service.OrderService;
import com.zxt.service.impl.AddressServiceImpl;
import com.zxt.service.impl.CartServiceImpl;
import com.zxt.service.impl.OrderServiceImpl;
import com.zxt.util.RandomUtil;
import com.zxt.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * projectName:myXiaoMi
 * author:ZhangXiaoTeng
 * time:2021/12/3 19:43
 * description:
 */

//订单预览
@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {

    //添加订单
    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //2.获取前端数据
        String aid = request.getParameter("aid");
        if (StringUtils.isEmpty(aid)) {
            request.setAttribute("msg", "发货id不能为空");
            return "/message.jsp";
        }

        try {
            //3.调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            //3.1获取购物车信息
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            if (cartList == null) {
                request.setAttribute("msg", "购物车不能为空");
                return "/message.jsp";
            }
            BigDecimal money = new BigDecimal(0);
            for (Cart cart : cartList) {
                money=money.add(cart.getMoney());
            }
            Order order = new Order(RandomUtil.createOrderId(), user.getId(), money, "1", new Date(), Integer.parseInt(aid));
            orderService.submitOrder(order, cartList);
            request.setAttribute("order", order);
            //转发成功
            return "/orderSuccess.jsp";
        } catch (Exception e) {
            request.setAttribute("msg", "添加订单失败" + e.getMessage());
            return "/message.jsp";
        }
    }


    //订单预览
    public String getOrderView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user=(User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //2.获取购物车的商品信息
        try {
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            //3.获取收货地址
            AddressService addressService = new AddressServiceImpl();
            List<Address> addList = addressService.find(user.getId());

            //4.存入域对象
            request.setAttribute("cartList", cartList);
            request.setAttribute("addList", addList);
            //5.转发重定向
            return "/order.jsp";
        }catch (Exception e){
            request.setAttribute("msg","预览订单失败"+e.getMessage());
            return "/message.jsp";
        }
    }


    //查看用户所有订单
    public String getOrderList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }

        try {
            //3.调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            List<Order> orderList=orderService.find(user.getId());
            request.setAttribute("orderList",orderList);
            return "/orderList.jsp";
        } catch (Exception e) {
            request.setAttribute("msg", "查看订单失败" + e.getMessage());
            return "/message.jsp";
        }
    }


    //获取订单详情
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //2.获取前端数据
        String oid = request.getParameter("oid");
        if (StringUtils.isEmpty(oid)) {
            request.setAttribute("msg", "订单编号不能为空");
            return "/message.jsp";
        }

        try {
            //3.调用业务逻辑
            OrderService orderService = new OrderServiceImpl();
            //3.1通过订单号查询订单
            Order order = orderService.findByOid(oid);
            //3.2通过订单号查询订单详情
            List<OrderDetail> orderDetails = orderService.findDetail(oid);
            //存储数据
            request.setAttribute("order", order);
            request.setAttribute("orderDetails", orderDetails);
            return "orderDetail.jsp";
        }catch (Exception e){
            request.setAttribute("msg", "订单详情查询失败 :"+e.getMessage());
            return "/message.jsp";
        }
    }
}

